<?php

namespace MyApp\Controller;

use RedBean_Facade as R;

class RaffleController extends \SlimController\SlimController
{
    /**
     * 转向
     * 
     * @param integer $r 0,1,2,3
     */
    private function redirectResult()
    {
        $this->app->redirect($this->app->urlFor('raffleresult') . '?uid=' . $this->app->user->id);
    }
    
    public function doAction()
    {
        $app = $this->app;
        $raffleConfig = $app->config('raffle');
        $_SESSION['raffled_at'] = time();
        
        $user = $app->user;
        // 第一次抽1000金币，之后10W
        $cost = $user->nbraffled > 0 ? 100000 : 1000;
        // 用户金币是否足够
        if ($user->coins < $cost) {
            $app->redirect($app->urlFor('index'));
            exit;
        }
        
        // 下面全算用户抽了，所以金币是要扣掉的
        $user->coins -= $cost;
        $user->nbraffled += 1; // 第几次抽了
        R::store($user);
        
        $rewardedArr = R::getCol('select reward from raffleresult where uid = ? ', [ $user->id ]);
        if (count($rewardedArr) >= 3) {
            // 已经中过3次奖，不能再中了
            $this->redirectResult();
            exit;
        }
        
        $md = date('md');
        $pool = $raffleConfig['rewards']; // 奖池
        if (!isset($pool[$md])) { // 不在活动日期内不中奖
            $this->redirectResult();
        }
        
        $reward = 0;
        $chance = $user->nbraffled == 1 ? 8000 : $raffleConfig['chance'];
        $rnd = mt_rand(1, 10000);
        if ($rnd > $chance) { // 没中奖
            $this->redirectResult();
        }

        // 算中奖了，但是究竟中没中、中哪个奖还取决于奖品数量等因素
        $rnd2 = mt_rand(1, 4000);
        
        /*
        if ($rnd2 == 1 && !in_array(1, $rewardedArr)) { // 一等奖
            $nb1 = R::count('raffleresult', ' reward = 1 ');
            if ($nb1 < $pool[$md][1]) { // 到这一天的一等奖还有
                $reward = 1;
            }
        }*/
        
        // 一等奖中了但是没奖的给二等奖，一二等奖只能中一次
        if (!$reward && $rnd2 < 150 && !in_array(2, $rewardedArr)) {
            $nb2 = R::count('raffleresult', ' reward = 2 ');
            if ($nb2 < $pool[$md][2]) { // 到这一天的二等奖还有
                $reward = 2;
            }
        }
        if (!$reward) {
            // 只要三等奖还有就给
            $nb3 = R::count('raffleresult', ' reward = 3 ');
            if ($nb3 < $pool[$md][3]) { // 到这一天的三等奖还没中完
                $reward = 3;
            }
        }
        // 确实没奖了
        if (!$reward) {
            $this->redirectResult();
        }
        
        $rr = R::dispense('raffleresult');
        $rr->uid       = $user->id;
        $rr->reward    = $reward;
        $rr->createdAt = R::isoDateTime();
        R::store($rr);
        
        $_SESSION['reward'] = $reward;
        $this->redirectResult();
    }
    
    public function resultAction()
    {
        // 先抽奖再看结果
        if (!isset($_SESSION['raffled_at'])) {
            $this->app->redirect($this->app->urlFor('index'));
        }
        unset($_SESSION['raffled_at']);
        
        $reward = isset($_SESSION['reward']) ? $_SESSION['reward'] : 0;
        unset($_SESSION['reward']);
        $this->render('home/receipt', [ 'reward' => $reward ]);
    }
}